package com.yonyou.gmmc.service.points.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;

import org.apache.log4j.Logger;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.util.MapConverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.yonyou.gmmc.common.annotation.ParamValid;
import org.marketing.common.Response;
import com.yonyou.gmmc.service.points.constant.PointsConstant;
import com.yonyou.gmmc.service.points.model.IdrealerStatus;
import com.yonyou.gmmc.service.points.service.WxPayService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = PointsConstant.BASE_PATH + "/" + PointsConstant.VERSION+ "/pay")
@Api("微信支付相关API")
public class WxPayController {
	private Logger logger = Logger.getLogger(TicketController.class);
	@Autowired
	private WxPayService wxPayService;
	
	/**
	 * 微信公众号支付接口
	 * 
	 * @return
	 */
	@RequestMapping(value="/orderReqJson",method = RequestMethod.GET)
	@ApiOperation(value = "微信公众号支付接口", notes = "微信公众号支付接口")
	@ParamValid
	public Response<Map<String, Object>> orderReqJson(
			 @RequestParam(required=true,defaultValue="") String buyerOpenId
    		,@RequestParam(required=true,defaultValue="") String amount
			,@RequestParam(required=true,defaultValue="") String motormasterName
			,@RequestParam(required=true,defaultValue="") String plateNumber
			,@RequestParam(required=true,defaultValue="") String goodsName
			,@RequestParam(required=true,defaultValue="") String subject
			,@RequestParam(required=true,defaultValue="") String tradeMemo
			,@RequestParam(required=true,defaultValue="") String orderId
			,@RequestParam(required=true,defaultValue="") String operator) {
		logger.info("WxPayController.orderReqJson==>start...");
		try {
			Map<String,String> map = new HashMap<>();
			map.put("buyerOpenId", buyerOpenId);
			map.put("amount", amount);
			map.put("motormasterName", motormasterName);
			map.put("plateNumber", plateNumber);
			map.put("goodsName", goodsName);
			map.put("subject", subject);
			map.put("tradeMemo", tradeMemo);
			map.put("orderId", orderId);
			map.put("operator", operator);
			Map<String, Object> result = wxPayService.orderReqJson(map);
			logger.info("WxPayController.orderReqJson==>result:"+result);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("WxPayController.orderReqJson==>end...");
		}
	}
	/**
	 * 退款接口
	 * 
	 * @return
	 */
	@RequestMapping(value="/cancelTrans",method = RequestMethod.GET)
	@ApiOperation(value = "退款接口", notes = "退款接口")
	@ParamValid
	public Response<Map<String, Object>> cancelTrans(
			 @RequestParam(required=true,defaultValue="") String orderNote
    		,@RequestParam(required=true,defaultValue="") String refundPerson
			,@RequestParam(required=true,defaultValue="") String remark
			,@RequestParam(required=true,defaultValue="") String orderId) {
		logger.info("WxPayController.cancelTrans==>start...");
		try {
			Map<String,String> map = new HashMap<>();
			map.put("orderNote", orderNote);
			map.put("refundPerson", refundPerson);
			map.put("remark", remark);
			map.put("orderId", orderId);
			Map<String, Object> result = wxPayService.cancelTrans(map);
			logger.info("WxPayController.cancelTrans==>result:"+result);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("WxPayController.cancelTrans==>end...");
		}
	}
	/**
	 * 查询(付款/退款)接口
	 * 
	 * @return
	 */
	@RequestMapping(value="/queryTradeResult",method = RequestMethod.GET)
	@ApiOperation(value = "查询(付款/退款)接口", notes = "查询(付款/退款)接口")
	@ParamValid
	public Response<List<Map<String, Object>>> queryTradeResult(
			 @RequestParam(required=true,defaultValue="") List<String> tradeIds) {
		logger.info("WxPayController.queryTradeResult==>start...");
		try {
			List<Map<String, Object>> result = wxPayService.queryTradeResult(tradeIds);
			logger.info("WxPayController.queryTradeResult==>result:"+result);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("WxPayController.queryTradeResult==>end...");
		}
	}
	/**
	 * 取消支付接口（若订单支付成功，则不能进行取消支付）
	 * 
	 * @return
	 */
	@RequestMapping(value="/cancelOrder",method = RequestMethod.GET)
	@ApiOperation(value = "取消支付接口", notes = "取消支付接口")
	@ParamValid
	public Response<Map<String, Object>> cancelOrder(
			 @RequestParam(required=true,defaultValue="") String tradeId) {
		logger.info("WxPayController.cancelOrder==>start...");
		try {
			Map<String, Object> result = wxPayService.cancelOrder(tradeId);
			logger.info("WxPayController.cancelOrder==>result:"+result);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("WxPayController.cancelOrder==>end...");
		}
	}
	/**
	 * 支付状态主动通知接口
	 * 
	 * @author Administrator
	 * @date 2017年4月17日
	 * @return 
	 */
	@RequestMapping(value = "/payStatus", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "支付状态主动通知接口", notes = "支付状态主动通知接口")
	public @ResponseBody Map<String, Object> payStatus(ServletRequest IdrealerStatus) {
		logger.info("WxPayController.payStatus==>start...");
		Map<String, Object> param = MapConverUtil.RequestPO2Map(IdrealerStatus,new IdrealerStatus());
		Map<String, Object> map = wxPayService.payStatus(param);
		logger.info("WxPayController.payStatus==>end...");
		return map;
	}
}
